﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using FukiERP.Common.Constants;
using FukiERP.Helper;
using FukiERP.Services;
using FukiERP.Services.Helper;
using FukiERP.Services.DBContext;
using DBservice = FukiERP.Services.DBContext;
namespace FukiERP.Product
{
    public partial class FormProductBrand : Telerik.WinControls.UI.RadForm
    {
        int _currentID = 0;
        public FormProductBrand()
        {
            InitializeComponent();
        }

        private void ProductBrand_Load(object sender, EventArgs e) {
            LoadData();
        }

        private void LoadData() {
            grd_ProductBrand.Rows.Clear();
            FukiERPDataContext context = DataAccess.GetDBContext();
            List<DBservice.ProductBrand> brands = context.ProductBrands.ToList();
            grd_ProductBrand.DataSource = brands;
        }

        private void tbn_Save_Click(object sender, EventArgs e) {
            DBservice.ProductBrand brand = null;
            FukiERPDataContext context = DataAccess.GetDBContext();
            errorProvider1.Clear();
            bool isValid = ValidateValue.ValidateRequirement(errorProvider1, tbx_BrandName);
            if (!isValid) {
                MessageBox.Show(MessageConstants.GaneralValidateMessage);
                return;
            }
            if (_currentID != 0) {
                brand = context.ProductBrands.Where(c => c.ID == _currentID).FirstOrDefault();
            }
            if (brand == null) {
                brand = new DBservice.ProductBrand();
                context.ProductBrands.InsertOnSubmit(brand);
            }
            brand.Name = tbx_BrandName.Text;
            brand.Description = tbx_BrandDes.Text;
            context.SubmitChanges();
            RefreshForm();
            LoadData();
        }

        private void btn_Delete_Click(object sender, EventArgs e) {
            if (_currentID != 0 && PresentHelper.ShowConfirmBox(MessageConstants.ConfirmDeleteMessage)) {
                FukiERPDataContext context = DataAccess.GetDBContext();
                DBservice.ProductBrand brand = context.ProductBrands.Where(c => c.ID == _currentID).FirstOrDefault();
                if (brand != null) {
                    context.ProductBrands.DeleteOnSubmit(brand);
                    context.SubmitChanges();
                }
               
                MessageBox.Show(MessageConstants.DeleteSuccessMessage);
                RefreshForm();
                LoadData();
            }
        }

        private void btn_Create_Click(object sender, EventArgs e) {
            RefreshForm();
        }

        private void RefreshForm() {
            _currentID = 0;
            tbx_BrandName.Text = string.Empty;
            tbx_BrandDes.Text = string.Empty;
        }

        private void grd_ProductBrand_CellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e) {
            _currentID = int.Parse(grd_ProductBrand.Rows[e.RowIndex].Cells[0].Value.ToString());
            DBservice.ProductBrand brand = (DBservice.ProductBrand)grd_ProductBrand.Rows[e.RowIndex].DataBoundItem;
            tbx_BrandName.Text = brand.Name;
            tbx_BrandDes.Text = brand.Description;
        }
    }
}
